import superagent from 'superagent';
import cheerio from 'cheerio';

class Crawler {
    private url = `http://toscrape.com/`;
    constructor() {
        this.getComposition();
    }
    // 爬取html
    async getComposition():Promise<void> {

        const respRet = await superagent.get(this.url);
        const respHtml = respRet.text;
        this.getTargeData(respHtml)
    }
    // 获取内容
    getTargeData(html: string): void {
        const $ = cheerio.load(html);
        // 先找到 tr，然后拿 a 和 第二个rd 里的内容
        const trList = $($('.table')[1]).find('tr');
        trList.map((index, item) => {
            if (index > 0) {
                const title = $(item).find('a').text();
                const desc = $(item).find('td').text();
                console.log('title', title);
                console.log('desc', desc);
            }
        });
    }
}

const c1 = new Crawler();